/*
 *  Gurr - A multiprotocol chat client for Android
 *  Copyright (C) 2011 Malte Aschermann, Wilfried Ramaker
 *  
 *  This file is part of Gurr.
 *  
 *  Gurr is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  Gurr is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Gurr.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.masc.gurr.model;

import java.util.HashMap;
import java.util.Iterator;
import java.util.TimerTask;

import com.masc.gurr.Controller;

import net.kano.joscar.ssiitem.BuddyItem;

public class Contact implements Comparable<Contact> {

	private String name;
	private int uid;
	private String uin;
	private String statusMsg;
	private ProtocolType type;
	private ContactStatus status;
	private Group fGroupRef;
	private BuddyItem myBuddyItem;
	private HashMap<String,String> fUiMap;
	private int fIdleMins = -1;
	private TimerTask fIdleTimerTask;
	
	public Contact(String uin, ProtocolType type) {
		this.uin = uin;
		this.type = type;
		status = new ContactStatus(-1);
		statusMsg="";
	}
	public Contact(int uid, ProtocolType type) {
		this.uid = uid;
		this.type = type;
		status = new ContactStatus(-1);
		statusMsg="";
	}
	
	
	public Contact(String uin, String name, ProtocolType type) {
		this.uin = uin;
		this.name = name;
		this.type = type;
		status = new ContactStatus(-1);
		statusMsg="";
		uid=-1;
	}
	
	public Contact(String uin, String name, ProtocolType type, String pStrStatusMsg) {
		this.uin = uin;
		this.name = name;
		this.type = type;
		statusMsg = pStrStatusMsg;
		status = new ContactStatus(-1);
		statusMsg="";
		uid=-1;
	}
	
	public Contact(String uin, String name, ContactStatus contactStatus, ProtocolType type) {
		this.uin = uin;
		this.name = name;
		this.status = contactStatus;
		this.type = type;
		statusMsg="";
		uid=-1;
	}
	
	public Contact(String uin, int uid, String name, ProtocolType type, String pStrStatusMsg) {
		this.uin = uin;
		this.name = name;
		this.type = type;
		status = new ContactStatus(-1);
		statusMsg="";
		this.uid = uid;
		statusMsg = pStrStatusMsg;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;		
	}

	public String getStatusMsg() {
		return statusMsg;
	}

	public void setStatusMsg(String statusMsg) {
		this.statusMsg = statusMsg;
	}

	public String getUin() {
		return uin;
	}
	
	public ProtocolType getType() {
		return type;
	}

	public String getStatus() {
		return status.toString();
	}

	public void setStatus(long status) {
		
		if (fUiMap != null)
			fUiMap.put("contactStatus", (new ContactStatus(status)).toString());

		
		this.status.setStatus(status);
	}
	
	public void setGroupRef(Group group) {
		this.fGroupRef = group;
	}
	
	public Group getGroupRef() {
		return this.fGroupRef;
	}

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}
	
	public void setUin(String uin) {
		/* arrr uin change party: 
		 * now we need to update all maps referencing this contact by his old uin 
		 */
		
		/* remove current map entries if parent group exists */
		if (this.fGroupRef != null)
			this.fGroupRef.removeContactFromMap(this);
		
		/* set new name */
		this.uin = uin;
		
		/* update maps w/ new name */
		if (this.fGroupRef != null)
			this.fGroupRef.addContactToMap(this);
	}
	public BuddyItem getMyBuddyItem() {
		return myBuddyItem;
	}
	public void setMyBuddyItem(BuddyItem myBuddyItem) {
		this.myBuddyItem = myBuddyItem;
	}
	
	public HashMap<String, String> getUiMap() {
		return fUiMap;
	}
	
	public void setUiMap(HashMap<String, String> pUiMap) {
		fUiMap = pUiMap;
	}
	

	public int compareTo(Contact another) {
		if (another == null)
			return 0;
		
		if (this.name.compareToIgnoreCase(another.name) < 0) {
	        return -1;
	      } else if (this.name.compareToIgnoreCase(another.name) > 0) {
	        return 1;
	      } else { }
		return 0;
	}
	
	public void setIdleMins(int idleMins) {
		fIdleMins  = idleMins;
	}
	public int getIdleMins() {
		return fIdleMins;
	}
	
	public TimerTask getIdleTimerTask() {
		return fIdleTimerTask;
	}
	
	public TimerTask getNewIdleTimerTask() {
		System.out.println("Contact:" + getUin() + " (" + getName() +"): start new idle timer");

		fIdleTimerTask = new TimerTask() {
			
			@Override
			public void run() {
				if (getIdleMins() != -1) { /* increment idle time only if contact is actually idle :) */
					setIdleMins(getIdleMins()+1);
					
					/* refresh contact list ui */
					if (Controller.getInstance().getContactsActivity() != null){
						Runnable run = new Runnable(){
							public void run(){
								Controller.getInstance().getContactsActivity().onContentChanged();
							}
						};
						Controller.getInstance().getContactsActivity().runOnUiThread(run);
					}
				}
			}
		};
		
		return fIdleTimerTask;
	}
	
	public void setIdleTimerTask(TimerTask pTimerTask) {
		fIdleTimerTask = pTimerTask;
	}
}
